<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('Controller', 'Controller');
App::uses('IniReader', 'Configure');
App::uses('AppConst', 'Lib');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	public $components = array(
			'Session',
            'Auth' => array(
					'loginAction' => array(
							'controller' => 'Users',
							'action' => 'login'
					),
 
  				   'authorize' => array(
									'Actions' => array('actionPath' => 'controllers')
							), 
					'authenticate' => array(
							'Jingbee'=>array(
							)
					),
					'className' => 'Permission'
			),
            'Permission'
	);
	public $uses = array('CategoryUser');
	public $pageTitle;
	public $helpers = array('Link','Html','Session','Form');
	public function __construct(CakeRequest $request= null,CakeResponse $response = null){
		parent::__construct($request, $response);
	}
	public function beforeRender() {
		parent::beforeRender();
        $this->params['prefix'] = false;
		$this->set('title_for_layout', $this->pageTitle);
	}
	/**
     * Overwrite check login and set layout
     * @content Check layout view unlogin and login
     */
	function beforeFilter() {
	   $userInfo[] = $this->Auth->user();
	   foreach($userInfo AS $itemUser){
	   	$star = 0;
	   	if($itemUser ['total_rate'] != 0)
	   		$star = round($itemUser ['total_star']/$itemUser ['total_rate'],0);
	   	if($star == 0){
	   		$class = array('star-no-view');
	   	}elseif($star == 5){
	   		$class = array('star-view');
	   	}else{
	   		for($i=0;$i<$star;$i++){
	   			$class[] = 'star-view-one';
	   		}
	   		for($i=0;$i<(5-$star);$i++){
	   			$class[] = 'star-no-view-one';
	   		}
	   	}
	   	$arrDataUserReturn    =   array(
	   			'id'				=>  	 $itemUser ['id'],
	   			'skype'				=>  	 $itemUser ['skype'],
	   			'link'              =>      '/Consultants/index?uid='.$itemUser ['id'],
	   			'avatar'            =>      $itemUser ['avata'],
	   			'full_name'         =>      $itemUser ['full_name'],
	   			'star_class'        =>      $class,
	   			'total_save_user'	=>		$itemUser ['total_save_user'],
	   			'consulting_fees'	=>      $itemUser['consulting_fees'],
	   			'group_id'			=>		$itemUser ['group_id'],
	   			'total_vote'        =>      $itemUser ['total_rate'],
	   			'total_comment'     =>      $itemUser ['total_comment'],
	   			'total_save'        =>      $itemUser ['total_save'],
	   			'stats_online'      =>      $itemUser ['stats_online'],
	   			'list_cat'          =>      @$arrUCatId[$itemUser ['id']],
	   			'date_reg_counse'   =>      date("d/m/Y G:i:s",$itemUser ['date_reg_counse'])
	   	);
	   	$arrCate = $this->CategoryUser->find('all',array('conditions'=>array('user_id'=>$itemUser ['id'])));
	   }
	   $this->set('arrCate',$arrCate);
	   $this->set('userCurrent',$arrDataUserReturn);
        parent::beforeFilter();
        //$arrControllerActionCheck = array(
//                                    //'admin'               =>  array('*')
//                                );
//        $controllerCurrent = strtolower($this->params['controller']);
//        $actionCurrent = strtolower($this->params['action']);
//        $checkDefault = false;
//        if(isset($arrControllerActionCheck[$controllerCurrent])){
//            $listActionCurrent = $arrControllerActionCheck[$controllerCurrent];
//            $checkDefault = (
//            			$listActionCurrent == array('*') ||
//            			in_array($actionCurrent, array_map('strtolower', $listActionCurrent))
//            		);
//        } 
//		if($checkDefault){
//            $this->layout = 'default';
//		}else{
            //$this->Auth->allow(array('login','logout'));
//    		$this->settingAuth();
//    		
//    		parent::beforeFilter();
//    		$authUser = $this->Auth->user();
//    		//debug($authUser);
//            if (isset($authUser['group_id']) && $authUser['group_id'] == AppConst::GROUP_ADMIN){
//    			$this->layout = 'admin';
//            } else{
//            	$this->layout = 'default';
//            }
		//}
	}
    protected function settingAuth(){ 
		$prefix = isset($this->params['prefix']) ? $this->params['prefix']: '';
		if(! $prefix){
			$this->Auth->loginAction = '/';
			$this->Auth->loginRedirect ='/';
			$this->Auth->logoutRedirect = '/';
			$this->Auth->accessDeniedUrl = '/403';
			return true;
		}
		// settings
		switch ($prefix) {
			case 'admin':
				$this->Auth->loginAction = 'Users/login';
				$this->Auth->loginRedirect ='/admin';
				$this->Auth->logoutRedirect = '/admin';
                $this->Auth->accessDeniedUrl = '/403';                
				break;
		}
		
		return true;
	}
}
